<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">
      <el-form-item label="请选择文档" prop="parentId">
        <el-select v-model="queryParams.parentId" placeholder="请选择文档" clearable filterable @change="handleQuery" style="width: 260px">
          <el-option v-for="item in docsOptions" :key="item.id" :label="item.title" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%;"
    >
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评论内容" align="left" prop="content" :show-overflow-tooltip="true"/>
      <el-table-column label="评论人" align="center" prop="fromUserName"/>
      <el-table-column label="被评论人" align="center" prop="toUserName"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180"/>
      <!-- likeNum 字段按需求隐藏，不展示 -->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize" @pagination="getList"/>
  </div>
</template>

<script>

import {listComments, delComments} from "@/api/official/comment"
import {listDocs} from "@/api/official/docs"

export default {
  name: "Comment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 文档下拉选项
      docsOptions: [],
      pageNum: 1,
      pageSize: 10,
      // 查询参数
      queryParams: {
        parentId: undefined,
        pageNum: 1,
        pageSize: 10
      }
    }
  },
  created() {
    this.loadDocs()
    this.getList()
  },
  methods: {
    /** 加载文档下拉 */
    loadDocs(){
      listDocs({pageNum: 1, pageSize: 99999}).then(res => {
        this.docsOptions = res.rows || []
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      const params = {
        parentId: this.queryParams.parentId,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      listComments(params).then(response => {
        this.list = response.rows || []
        this.total = response.total || 0
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    handleDelete(row){
      this.$modal.confirm('是否确认删除该评论？').then(() => {
        return delComments(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    }
  }
}
</script>

